package com.sec.android.app.myfiles.info;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.util.SparseArray;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.util.DrawableMgr;
import com.sec.android.app.myfiles.util.DrmUtils;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.FolderAppIconMgr;
import com.sec.android.app.myfiles.util.SemFwWrapper;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

/**
 * MediaScanner helper class. {@hide}
 */
public class MediaFile {

    private static final String TAG = "MediaFile";

    private static HashMap<String, MediaFileInfo> sExtensionToMediaFileTypeMap = new HashMap<>();
    private static SparseArray<MediaFileInfo> sFileTypeToMediaFileTypeMap = new SparseArray<>();
    private static HashMap<String, Integer> sMimeTypeToFileTypeMap = new HashMap<>();

    private static ArrayList<String> sDocumentExtensions = new ArrayList<>();

    public static final int FILE_ICON_DEFAULT = R.drawable.myfiles_list_etc;

    public static class MediaFileInfo {
        public int fileType;
        public String extension;
        public String mimeType;
        public int icon;

        MediaFileInfo(int fileType, String extension, String mimeType, int icon) {
            this.fileType = fileType;
            this.extension = extension;
            this.mimeType = mimeType;
            this.icon = icon;
        }
    }

    static void addFileType(String extension, int fileType, String mimeType, int icon) {

        MediaFileInfo mediaFileInfo = new MediaFileInfo(fileType, extension, mimeType, icon);

        sExtensionToMediaFileTypeMap.put(extension, mediaFileInfo);
        sFileTypeToMediaFileTypeMap.put(fileType, mediaFileInfo);
        addMimeTypeToFileTypeMap(mimeType, fileType);

        if (FileType.isDocumentFileType(fileType)) {
            sDocumentExtensions.add(extension);
        }
    }

    static void addMimeTypeToFileTypeMap(String mimeType, int fileType) {
        if (!sMimeTypeToFileTypeMap.containsKey(mimeType)) {
            sMimeTypeToFileTypeMap.put(mimeType, fileType);
        }
    }

    static {
        // icon - myfiles_list_amr
        addFileType("M4A", FileType.M4A, "audio/mp4", R.drawable.myfiles_list_amr);
        addFileType("AMR", FileType.AMR, "audio/amr", R.drawable.myfiles_list_amr);
        addFileType("AWB", FileType.AWB, "audio/amr-wb", R.drawable.myfiles_list_amr);
        addFileType("3GA", FileType._3GA, "audio/3gpp", R.drawable.myfiles_list_amr);

        // icon - myfiles_list_apk
        addFileType("APK", FileType.APK, "application/vnd.android.package-archive", R.drawable.myfiles_list_apk);

        // icon - myfiles_list_contact
        addFileType("VCF", FileType.VCF, "text/x-vcard", R.drawable.myfiles_list_contact);

        // icon - myfiles_list_eml
        addFileType("EML", FileType.EML, "message/rfc822", R.drawable.myfiles_list_eml);

        // icon - myfiles_list_gallery
        addFileType("JPG", FileType.JPG, "image/jpeg", R.drawable.myfiles_list_gallery);
        addFileType("JPEG", FileType.JPEG, "image/jpeg", R.drawable.myfiles_list_gallery);
        addFileType("MY5", FileType.MY5, "image/vnd.tmo.my5", R.drawable.myfiles_list_gallery);
        addFileType("GIF", FileType.GIF, "image/gif", R.drawable.myfiles_list_gallery);
        addFileType("PNG", FileType.PNG, "image/png", R.drawable.myfiles_list_gallery);
        addFileType("BMP", FileType.XBMP, "image/x-ms-bmp", R.drawable.myfiles_list_gallery);
        /* Google Drive Response - "title": "BMP_4825X972 13.4MB.bmp","mimeType": "image/bmp",*/
        addFileType("BMP", FileType.BMP, "image/bmp", R.drawable.myfiles_list_gallery);

        addFileType("WBMP", FileType.WBMP, "image/vnd.wap.wbmp", R.drawable.myfiles_list_gallery);
        addFileType("WEBP", FileType.WEBP, "image/webp", R.drawable.myfiles_list_gallery);
        addFileType("GOLF", FileType.GOLF, "image/golf", R.drawable.myfiles_list_gallery);

        // icon - myfiles_list_html
        addFileType("HTML", FileType.HTML, "text/html", R.drawable.myfiles_list_html);
        addFileType("HTM", FileType.HTM, "text/html", R.drawable.myfiles_list_html);
        addFileType("XHTML", FileType.XHTML, "text/html", R.drawable.myfiles_list_html);
        addFileType("XML", FileType.XML, "application/xhtml+xml", R.drawable.myfiles_list_html);
        addFileType("WGT", FileType.WGT, "application/vnd.samsung.widget", R.drawable.myfiles_list_html);

        // icon - myfiles_list_hwp
        addFileType("HWP", FileType.HWP, "application/x-hwp", R.drawable.myfiles_list_hwp);
        addFileType("HWPX", FileType.HWPX, "application/vnd.hancom.hwpx", R.drawable.myfiles_list_hwp);
        addFileType("HWT", FileType.HWT, "application/haansofthwt", R.drawable.myfiles_list_hwp);

        // icon - myfiles_list_memo
        addFileType("MEMO", FileType.MEMO, "application/memo", R.drawable.myfiles_list_memo);

        // icon - myfiles_list_music
        addFileType("MP3", FileType.MP3, "audio/mpeg", R.drawable.myfiles_list_music);
        addFileType("WAV", FileType.WAV, "audio/x-wav", R.drawable.myfiles_list_music);
        addFileType("WMA", FileType.WMA, "audio/x-ms-wma", R.drawable.myfiles_list_music);
        addFileType("OGG", FileType.OGG, "audio/ogg", R.drawable.myfiles_list_music);
        addFileType("OGA", FileType.OGA, "application/ogg", R.drawable.myfiles_list_music);
        addFileType("AAC", FileType.AAC, "audio/aac", R.drawable.myfiles_list_music);
        addFileType("FLAC", FileType.FLAC, "audio/flac", R.drawable.myfiles_list_music);
        addFileType("MP4_A", FileType.MP4_A, "audio/mp4", R.drawable.myfiles_list_music);
        addFileType("MP4A", FileType.MP4_AUDIO, "audio/mp4", R.drawable.myfiles_list_music);
        addFileType("MPGA", FileType.MPGA, "audio/mpeg", R.drawable.myfiles_list_music);
        addFileType("3GP_A", FileType._3GP_AUDIO, "audio/3gpp", R.drawable.myfiles_list_music);
        addFileType("3G2_A", FileType._3G2_AUDIO, "audio/3gpp2", R.drawable.myfiles_list_music);
        addFileType("ASF_A", FileType.ASF_AUDIO, "audio/x-ms-asf", R.drawable.myfiles_list_music);
        addFileType("3GPP_A", FileType._3GPP_AUDIO, "audio/3gpp", R.drawable.myfiles_list_music);
        addFileType("MID", FileType.MID, "audio/midi", R.drawable.myfiles_list_music);
        addFileType("MID_A", FileType.MID_A, "audio/mid", R.drawable.myfiles_list_music);
        addFileType("MIDI", FileType.MIDI, "audio/midi", R.drawable.myfiles_list_music);
        addFileType("RTX", FileType.RTX, "audio/midi", R.drawable.myfiles_list_music);
        addFileType("OTA", FileType.OTA, "audio/midi", R.drawable.myfiles_list_music);
        addFileType("XMF", FileType.XMF, "audio/midi", R.drawable.myfiles_list_music);
        addFileType("MXMF", FileType.MXMF, "audio/midi", R.drawable.myfiles_list_music);
        addFileType("RTTTL", FileType.RTL, "audio/midi", R.drawable.myfiles_list_music);
        addFileType("SMF", FileType.SMF, "audio/sp-midi", R.drawable.myfiles_list_music);
        addFileType("SPMID", FileType.SPMID, "audio/sp-midi", R.drawable.myfiles_list_music);
        addFileType("IMY", FileType.IMY, "audio/imelody", R.drawable.myfiles_list_music);
        addFileType("MKA", FileType.MKA, "audio/x-matroska", R.drawable.myfiles_list_music);
        addFileType("PYA", FileType.PYA, "audio/vnd.ms-playready.media.pya", R.drawable.myfiles_list_music);
        addFileType("QCP", FileType.QCP, "audio/qcelp", R.drawable.myfiles_list_music);
        if (AppFeatures.DSD_ENABLED) {
            addFileType("DSF", FileType.DSF, "audio/x-dsf", R.drawable.myfiles_list_music);
            addFileType("DFF", FileType.DFF, "audio/x-dff", R.drawable.myfiles_list_music);
        }
        if (AppFeatures.APE_ENABLED) {
            addFileType("APE", FileType.APE, "audio/x-ape", R.drawable.myfiles_list_music);
        }

        // icon - myfiles_list_pdf
        addFileType("PDF", FileType.PDF, "application/pdf", R.drawable.myfiles_list_pdf);

        // icon - myfiles_list_ppt
        addFileType("PPS", FileType.PPS, "application/vnd.ms-powerpoint", R.drawable.myfiles_list_ppt);
        addFileType("PPT", FileType.PPT, "application/vnd.ms-powerpoint", R.drawable.myfiles_list_ppt);
        addFileType("PPTX", FileType.PPTX, "application/vnd.openxmlformats-officedocument.presentationml.presentation", R.drawable.myfiles_list_ppt);
        addFileType("PPTM", FileType.PPTM, "application/vnd.ms-powerpoint.presentation.macroEnabled.12", R.drawable.myfiles_list_ppt);
        addFileType("POT", FileType.POT, "application/vnd.ms-powerpoint", R.drawable.myfiles_list_ppt);
        addFileType("POTX", FileType.POTX, "application/vnd.openxmlformats-officedocument.presentationml.template", R.drawable.myfiles_list_ppt);
        addFileType("PPSX", FileType.PPSX, "application/vnd.openxmlformats-officedocument.presentationml.slideshow", R.drawable.myfiles_list_ppt);
        addFileType("", FileType.PPT, MimeType.GOOGLE_SLIDES, R.drawable.myfiles_list_ppt);

        // icon - myfiles_list_s_planner
        addFileType("VCS", FileType.VCS, "text/x-vCalendar", R.drawable.myfiles_list_s_planner);
        addFileType("ICS", FileType.ICS, "text/calendar", R.drawable.myfiles_list_s_planner);

        // icon - myfiles_list_scrapbook
        addFileType("SCC", FileType.SCC_SCRAP, "application/vnd.samsung.scc.pinall", R.drawable.myfiles_list_scrapbook);

        // icon - myfiles_list_snb
        addFileType("SNB", FileType.SNB, "application/snb", R.drawable.myfiles_list_snb);

        // icon - myfiles_list_spd
        addFileType("SPD", FileType.SPD, "application/spd", R.drawable.myfiles_list_spd);

        // icon - myfiles_list_storyalbum
        addFileType("SCC", FileType.SCC, "application/vnd.samsung.scc.storyalbum", R.drawable.myfiles_list_storyalbum);
        addFileType("SFF", FileType.SFF, "application/vnd.samsung.scc.storyalbum", R.drawable.myfiles_list_storyalbum);  //addition
        addFileType("SSF", FileType.SSF, "application/ssf", R.drawable.myfiles_list_storyalbum);

        // icon - myfiles_list_task
        addFileType("VTS", FileType.VTS, "text/x-vtodo", R.drawable.myfiles_list_task);

        // icon - myfiles_list_txt
        addFileType("ASC", FileType.ASC, "text/plain", R.drawable.myfiles_list_txt);
        addFileType("TXT", FileType.TXT, "text/plain", R.drawable.myfiles_list_txt);
        addFileType("EPUB", FileType.EPUB, "application/epub+zip", R.drawable.myfiles_list_txt);
        addFileType("ACSM", FileType.ACSM, "application/vnd.adobe.adept+xml", R.drawable.myfiles_list_txt);

        // icon - myfiles_list_video
        addFileType("MPEG", FileType.MPEG, "video/mpeg", R.drawable.myfiles_list_video);
        addFileType("MPG", FileType.MPG, "video/mpeg", R.drawable.myfiles_list_video);
        addFileType("MP4", FileType.MP4, "video/mp4", R.drawable.myfiles_list_video);
        addFileType("M4V", FileType.M4V, "video/mp4", R.drawable.myfiles_list_video);
        addFileType("3GP", FileType._3GP, "video/3gpp", R.drawable.myfiles_list_video);
        addFileType("3GPP", FileType._3GPP, "video/3gpp", R.drawable.myfiles_list_video);
        addFileType("3G2", FileType._3G2, "video/3gpp2", R.drawable.myfiles_list_video);
        addFileType("3GPP2", FileType._3GPP2, "video/3gpp2", R.drawable.myfiles_list_video);
        addFileType("WMV", FileType.WMV, "video/x-ms-wmv", R.drawable.myfiles_list_video);
        addFileType("ASF", FileType.ASF, "video/x-ms-asf", R.drawable.myfiles_list_video);
        addFileType("AVI", FileType.AVI, "video/avi", R.drawable.myfiles_list_video);
        addFileType("DIVX", FileType.DIVX, "video/divx", R.drawable.myfiles_list_video);
        addFileType("FLV", FileType.FLV, "video/flv", R.drawable.myfiles_list_video);
        addFileType("MKV", FileType.MKV, "video/x-matroska", R.drawable.myfiles_list_video);
        addFileType("SDP", FileType.SDP, "application/sdp", R.drawable.myfiles_list_video);
        addFileType("TS", FileType.TS, "video/mp2ts", R.drawable.myfiles_list_video);
        addFileType("PYV", FileType.PYV, "video/vnd.ms-playready.media.pyv", R.drawable.myfiles_list_video);
        addFileType("MOV", FileType.MOV, "video/quicktime", R.drawable.myfiles_list_video);
        addFileType("SKM", FileType.SKM, "video/skm", R.drawable.myfiles_list_video);
        addFileType("K3G", FileType.K3G, "video/k3g", R.drawable.myfiles_list_video);
        addFileType("AK3G", FileType.AK3G, "video/ak3g", R.drawable.myfiles_list_video);
        addFileType("WEBM", FileType.WEBM, "video/webm", R.drawable.myfiles_list_video);
        addFileType("MTS", FileType.MTS, "video/mp2ts", R.drawable.myfiles_list_video);
        addFileType("M2TS", FileType.M2TS, "video/mp2ts", R.drawable.myfiles_list_video);
        addFileType("M2T", FileType.M2T, "video/mp2ts", R.drawable.myfiles_list_video);
        addFileType("TRP", FileType.TRP, "video/mp2ts", R.drawable.myfiles_list_video);
        addFileType("TP", FileType.TP, "video/mp2ts", R.drawable.myfiles_list_video);

        // icon - myfiles_list_vtext
        addFileType("VNT", FileType.VNT, "text/x-vnote", R.drawable.myfiles_list_vtext);

        // icon - myfiles_list_word
        addFileType("RTF", FileType.RTF, "application/rtf", R.drawable.myfiles_list_word);
        addFileType("DOC", FileType.DOC, "application/msword", R.drawable.myfiles_list_word);
        addFileType("DOCX", FileType.DOCX, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", R.drawable.myfiles_list_word);
        addFileType("DOCM", FileType.DOCM, "application/vnd.ms-word.document.macroEnabled.12", R.drawable.myfiles_list_word);
        addFileType("DOT", FileType.DOT, "application/msword", R.drawable.myfiles_list_word);
        addFileType("DOTX", FileType.DOTX, "application/vnd.openxmlformats-officedocument.wordprocessingml.template", R.drawable.myfiles_list_word);
        addFileType("", FileType.DOC, MimeType.GOOGLE_DOCS, R.drawable.myfiles_list_word);
        addFileType("HWDT", FileType.HWDT, "application/hancomhwdt", R.drawable.myfiles_list_word);

        // icon - myfiles_list_xls
        addFileType("CSV", FileType.CSV, "text/comma-separated-values", R.drawable.myfiles_list_xls);
        addFileType("XLS", FileType.XLS, "application/vnd.ms-excel", R.drawable.myfiles_list_xls);
        addFileType("XLSX", FileType.XLSX, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", R.drawable.myfiles_list_xls);
        addFileType("XLT", FileType.XLT, "application/vnd.ms-excel", R.drawable.myfiles_list_xls);
        addFileType("XLTX", FileType.XLTX, "application/vnd.openxmlformats-officedocument.spreadsheetml.template", R.drawable.myfiles_list_xls);
        addFileType("XLSM", FileType.XLSM, "application/vnd.ms-excel.sheet.macroEnabled.12", R.drawable.myfiles_list_xls);
        addFileType("", FileType.XLS, MimeType.GOOGLE_SHEETS, R.drawable.myfiles_list_xls);

        // icon - myfiles_list_zip
        addFileType("ZIP", FileType.ZIP, "application/zip", R.drawable.myfiles_list_zip);

        // icon - myfiles_list_sdoc
        addFileType("SDOC", FileType.SDOC, "application/sdoc", R.drawable.myfiles_list_sdoc);

        // icon - myfiles_list_lock
        addFileType("ENC", FileType.ENC, "application/enc", R.drawable.myfiles_list_etc);
        addFileType("LOC", FileType.LOC, "application/loc", R.drawable.myfiles_list_etc);

        // icon - myfiles_list_etc
        addFileType("M3U", FileType.M3U, "audio/x-mpegurl", R.drawable.myfiles_list_etc);
        addFileType("PLS", FileType.PLS, "audio/x-scpls", R.drawable.myfiles_list_etc);
        addFileType("WPL", FileType.WPL, "application/vnd.ms-wpl", R.drawable.myfiles_list_etc);
        addFileType("SWF", FileType.SWF, "application/x-shockwave-flash", R.drawable.myfiles_list_etc);
        addFileType("SVG", FileType.SVG, "image/svg+xml", R.drawable.myfiles_list_etc);
        addFileType("DCF", FileType.DCF, "application/vnd.oma.drm.content", R.drawable.myfiles_list_etc);
        addFileType("ODF", FileType.ODF, "application/vnd.oma.drm.content", R.drawable.myfiles_list_etc);
        addFileType("SM4", FileType.SM4, "video/vnd.sdrm-media.sm4", R.drawable.myfiles_list_etc);
        addFileType("JAD", FileType.JAD, "text/vnd.sun.j2me.app-descriptor", R.drawable.myfiles_list_etc);
        addFileType("JAR", FileType.JAR, "application/java-archive", R.drawable.myfiles_list_etc);
        addFileType("SASF", FileType.SASF, "application/x-sasf", R.drawable.myfiles_list_etc);
        addFileType("SOL", FileType.SOL, "application/com.sec.kidspiano", R.drawable.myfiles_list_etc);
        // certificate
        addFileType("P12", FileType.P12, "application/x-pkcs12", R.drawable.myfiles_list_etc);
        addFileType("PFX", FileType.PFX, "application/x-pkcs12", R.drawable.myfiles_list_etc);
        addFileType("CRT", FileType.CRT, "application/x-x509-ca-cert", R.drawable.myfiles_list_etc);
        addFileType("DER", FileType.DER, "application/x-x509-ca-cert", R.drawable.myfiles_list_etc);
        addFileType("PEM", FileType.PEM, "application/x-pem-file", R.drawable.myfiles_list_etc);
        addFileType("CER", FileType.CER, "application/pkix-cert", R.drawable.myfiles_list_etc);
        addFileType("", FileType.UNKNOWN, MimeType.DEFAULT, R.drawable.myfiles_list_etc);
        // mimeType to fileType (Same FileType & Different MimeType)
        addMimeTypeToFileTypeMap("video/mp2p", FileType.MPG);
        addMimeTypeToFileTypeMap("audio/m4a", FileType.M4A);
        addMimeTypeToFileTypeMap("audio/mp4a-latm", FileType.AAC);
        // Add for iWorks document files
        addFileType("PAGES", FileType.PAGES, "application/x-iwork-pages-sffpages", R.drawable.myfiles_list_etc);
        addFileType("KEY", FileType.KEY, "application/x-iwork-keynote-sffkey", R.drawable.myfiles_list_etc);
        addFileType("NUMBERS", FileType.NUMBERS, "application/x-iwork-numbers-sffnumbers", R.drawable.myfiles_list_etc);
        addFileType("LA", FileType.LA, "application/octet-stream", R.drawable.myfiles_list_etc);
        // Add for gpx file
        addFileType("GPX", FileType.GPX, "application/gpx+xml", R.drawable.myfiles_list_etc);
        addFileType("SHOW", FileType.SHOW, "application/hshow", R.drawable.myfiles_list_etc);
        addMimeTypeToFileTypeMap("application/x-hshow", FileType.SHOW);
        addFileType("XLSB", FileType.XLSB, "application/vnd.ms-excel.sheet.binary.macroenabled.12", R.drawable.myfiles_list_etc);
        addMimeTypeToFileTypeMap("application/vnd.ms-excel.sheet.binary.macroEnabled.12", FileType.XLSB);
        addFileType("XLTM", FileType.XLTM, "application/vnd.ms-excel.template.macroenabled.12", R.drawable.myfiles_list_etc);
        addMimeTypeToFileTypeMap("application/vnd.ms-excel.template.macroEnabled.12", FileType.XLTM);
        addFileType("CELL", FileType.CELL, "application/haansoftcell", R.drawable.myfiles_list_etc);
        addMimeTypeToFileTypeMap("application/hancomcell", FileType.CELL);
        addMimeTypeToFileTypeMap("application/hcell", FileType.CELL);
        addMimeTypeToFileTypeMap("application/vnd.hancom.hcell", FileType.CELL);
        addFileType("HCDT", FileType.HCDT, "application/haansofthcdt", R.drawable.myfiles_list_etc);
        addMimeTypeToFileTypeMap("application/hancomhcdt", FileType.HCDT);
        addFileType("PRN", FileType.PRN, "text/space-separated-values", R.drawable.myfiles_list_etc);
        addFileType("TSV", FileType.TSV, "text/tab-separated-values", R.drawable.myfiles_list_etc);
        // Raw Image File
        addFileType("RAF", FileType.RAF, "image/x-fuji-raf", R.drawable.myfiles_list_raw);
        addFileType("ORF", FileType.ORF, "image/x-olympus-orf", R.drawable.myfiles_list_raw);
        addFileType("ERF", FileType.ERF, "image/x-raw-epson", R.drawable.myfiles_list_raw);
        addFileType("FFF", FileType.FFF, "image/x-fff", R.drawable.myfiles_list_raw);
        addFileType("CRW", FileType.CRW, "image/x-canon-crw", R.drawable.myfiles_list_raw);
        addFileType("CR2", FileType.CR2, "image/x-canon-cr2", R.drawable.myfiles_list_raw);
        addFileType("CR3", FileType.CR3, "image/x-canon-cr3", R.drawable.myfiles_list_raw);
        addFileType("DNG", FileType.DNG, "image/x-adobe-dng", R.drawable.myfiles_list_raw);
        addFileType("MEF", FileType.MEF, "image/x-mef", R.drawable.myfiles_list_raw);
        addFileType("MOS", FileType.MOS, "image/x-raw-leaf", R.drawable.myfiles_list_raw);
        addFileType("PXN", FileType.PXN, "image/x-pxn", R.drawable.myfiles_list_raw);
        addFileType("SRW", FileType.SRW, "image/x-samsung-srw", R.drawable.myfiles_list_raw);
        addFileType("PTX", FileType.PTX, "image/x-ptx", R.drawable.myfiles_list_raw);
        addFileType("PEF", FileType.PEF, "image/x-pef", R.drawable.myfiles_list_raw);
        addFileType("RW2", FileType.RW2, "image/x-panasonic-rw2", R.drawable.myfiles_list_raw);
        addFileType("BAY", FileType.BAY, "image/x-bay", R.drawable.myfiles_list_raw);
        addFileType("TIF", FileType.TIF, "image/tiff", R.drawable.myfiles_list_raw);
        addFileType("K25", FileType.K25, "image/x-k25", R.drawable.myfiles_list_raw);
        addFileType("KDC", FileType.KDC, "image/x-kdc", R.drawable.myfiles_list_raw);
        addFileType("DCS", FileType.DCS, "image/x-dcs", R.drawable.myfiles_list_raw);
        addFileType("DCR", FileType.DCR, "image/x-dcr", R.drawable.myfiles_list_raw);
        addFileType("DRF", FileType.DRF, "image/x-drf", R.drawable.myfiles_list_raw);
        addFileType("ARW", FileType.ARW, "image/x-sony-arw", R.drawable.myfiles_list_raw);
        addFileType("SRF", FileType.SRF, "image/x-sony-srf", R.drawable.myfiles_list_raw);
        addFileType("SR2", FileType.SR2, "image/x-sony-sr2", R.drawable.myfiles_list_raw);
        addFileType("CAP", FileType.CAP, "image/x-cap", R.drawable.myfiles_list_raw);
        addFileType("IIQ", FileType.IIQ, "image/x-iiq", R.drawable.myfiles_list_raw);
        addFileType("MRW", FileType.MRW, "image/x-minolta-mrw", R.drawable.myfiles_list_raw);
        addFileType("X3F", FileType.X3F, "image/x-sigma-x3f", R.drawable.myfiles_list_raw);
        addFileType("R3D", FileType.R3D, "image/x-r3d", R.drawable.myfiles_list_raw);
        addFileType("NEF", FileType.NEF, "image/x-nikon-nef", R.drawable.myfiles_list_raw);
        addFileType("NRW", FileType.NRW, "image/x-nrw", R.drawable.myfiles_list_raw);

        // Add for iOS's PassBook file
        addFileType("PKPASS", FileType.PASSBOOK, "application/vnd.apple.pkpass", R.drawable.myfiles_list_etc);

        // Add for torrent file
        // (Reference Link) http://fileformats.archiveteam.org/wiki/Torrent_file
        addFileType("TORRENT", FileType.TORRENT, "application/x-bittorrent", R.drawable.myfiles_list_etc);

        // Add for RAR file
        addFileType("RAR", FileType.RAR, "application/x-rar-compressed", R.drawable.myfiles_list_etc);

        // XDW fuji xerox docuworks
        addFileType("XDW", FileType.XDW, "application/vnd.fujixerox.docuworks", R.drawable.myfiles_list_etc);
    }

    /**
     * <pre>
     * get MediaType by path.
     * It does not consider DRM contents.
     * if you want consider DRM contents, use {@link #getFileType(String, Context)}
     * </pre>
     *
     * @param path
     * @return
     */
    public static int getFileType(String path) {
        int nRet = FileType.UNKNOWN;

        MediaFileInfo info = getMediaFileInfo(path, true);
        if (info != null) {
            nRet = info.fileType;
        }

        return nRet;
    }

    /**
     * <pre>
     * get MediaType by path and mimeType.
     * </pre>
     *
     * @param path
     * @return
     */
    public static int getFileType(String path, String mimeType) {
        int nRet = FileType.UNKNOWN;

        MediaFileInfo info = getMediaFileInfo(path, mimeType);
        if (info != null) {
            nRet = info.fileType;
        }

        return nRet;
    }

    /**
     * <pre>
     * get MediaType include DRM contents
     * </pre>
     *
     * @param path
     * @param context
     * @return
     */
    public static int getFileType(String path, Context context) {
        int nRet = FileType.UNKNOWN;

        MediaFileInfo info = getMediaFileInfo(path, context);
        if (info != null) {
            nRet = info.fileType;
        }

        return nRet;
    }

    public static int getFileType(String path, boolean isSupportAvFile) {
        int nRet = FileType.UNKNOWN;
        MediaFileInfo info = getMediaFileInfo(path, isSupportAvFile);
        if (info != null) {
            nRet = info.fileType;
        }
        return nRet;
    }

    public static String getMimeType(String path) {
        String ret = MimeType.DEFAULT;

        MediaFileInfo info = getMediaFileInfo(path, true);
        if (info != null) {
            ret = info.mimeType;
        }

        return ret;
    }

    public static String getMimeType(Context context, String path) {
        String ret = MimeType.DEFAULT;

        MediaFileInfo info = getMediaFileInfo(path, context);
        if (info != null) {
            ret = info.mimeType;
        }

        return ret;
    }

    public static String getCommonShareMimeType(String fileMimeType, String commonMimeType) {
        if (commonMimeType == null || commonMimeType.equals(fileMimeType)) {
            return fileMimeType;
        } else {
            String file = getMimeCategory(fileMimeType);
            String common = getMimeCategory(commonMimeType);
            if (common.equals(file)) {
                return common;
            } else {
                return MimeType.ALL_CONTENTS;
            }
        }
    }

    public static String getMimeCategory(String mimeType) {
        if (mimeType == null) {
            mimeType = MimeType.ALL_CONTENTS;
        } else {
            int lastIndex = mimeType.lastIndexOf(MimeType.DIVIDER, mimeType.length() - 1);
            if (lastIndex == -1) {
                mimeType = MimeType.ALL_CONTENTS;
            } else {
                mimeType = mimeType.substring(0, lastIndex) + "/*";
            }
        }
        return mimeType;
    }

    public static int getIcon(Context context, FileRecord record) {
        if (record.getFileType() == FileType.FOLDER) {
            return R.drawable.myfiles_list_folder;
        }
        MediaFileInfo result = sFileTypeToMediaFileTypeMap.get(record.getFileType());
        if (result != null) {
            return result.icon;
        } else {
            return FILE_ICON_DEFAULT;
        }
    }

    public static int getIcon(String path) {
        int nRet = FILE_ICON_DEFAULT;

        MediaFileInfo info = getMediaFileInfo(path, true);
        if (info != null) {
            nRet = info.icon;
        }

        return nRet;
    }

    public static int getIcon(String path, Context context) {
        int nRet = FILE_ICON_DEFAULT;

        MediaFileInfo info = getMediaFileInfo(path, context);
        if (info != null) {
            nRet = info.icon;
        }

        return nRet;
    }

    public static int getIconbyMimeType(String mimeType) {
        int nRet = FILE_ICON_DEFAULT;

        int fileType = sMimeTypeToFileTypeMap.get(mimeType);
        MediaFileInfo info = sFileTypeToMediaFileTypeMap.get(fileType);
        if (info != null) {
            nRet = info.icon;
        }

        return nRet;
    }

    public static Drawable getFileTypeDrawable(Context context, FileRecord record) {
        int resId = getIcon(context, record);
        Drawable ret = DrawableMgr.getFileIconDrawable(context, resId);

        if (record.isDirectory()) {
            FolderAppIconMgr.FolderAppIconInfo info = null;
            if (FileUtils.isPrivateFolder(record.getFullPath())) {
                info = FolderAppIconMgr.getApplicationItem("/storage/Private");
            } else if (record.getStorageType() == FileRecord.StorageType.Local || record.getStorageType() == FileRecord.StorageType.Category) {
                info = FolderAppIconMgr.getApplicationItem(record.getFullPath());
            }
            if (info != null) {
                if (info != FolderAppIconMgr.getApplicationItem("/storage/Private")) {
                    ret = DrawableMgr.getFileIconDrawable(context, R.drawable.myfiles_list_folder_3rd);
                    ret = info.getMergedIcon(context, (BitmapDrawable) ret);
                    if (ret == null) {
                        ret = DrawableMgr.getFileIconDrawable(context, R.drawable.myfiles_list_folder);
                    }
                } else if (AppFeatures.isTablet()) {
                    ret = info.getMergedIconForTablet(context, (BitmapDrawable) ret);
                }
            }
        }
        return ret;
    }

    public static int getFileTypeForMimeType(String mimeType) {
        if (mimeType == null)
            return 0;

        Integer value = sMimeTypeToFileTypeMap.get(mimeType);
        return (value == null ? 0 : value);
    }

    public static ArrayList<MediaFileInfo> getMimeTypeFileInfo(String mimeType) {
        ArrayList<MediaFileInfo> ret = new ArrayList<>();

        for (int i = 0; i < sFileTypeToMediaFileTypeMap.size(); i++) {
            int key = sFileTypeToMediaFileTypeMap.keyAt(i);
            MediaFileInfo info = sFileTypeToMediaFileTypeMap.get(key);
            if (info != null) {
                if (isIncludedMimeType(info.mimeType, mimeType)) {
                    ret.add(info);
                }
            }
        }

        return ret;
    }

    /**
     * Check the mimeType is same or included
     *
     * @param mimeType fixed mimeType(is declared in hashMap)
     * @param compare  compare mimeType (ex: audio/mpgeg, audio/*)
     * @return
     */
    public static boolean isIncludedMimeType(String mimeType, String compare) {
        boolean bRet;
        int starIndex = compare.indexOf('*');

        if (starIndex >= 0) {
            String cmp = compare.substring(0, starIndex);
            bRet = mimeType.startsWith(cmp);
        } else {
            bRet = mimeType.equals(compare);
        }

        return bRet;
    }

    /**
     * <pre>
     * get MediaFileInfo.
     * It does not consider DRM contents.
     * if you want consider DRM contents, use {@link #getMediaFileInfo(String, Context)}
     * </pre>
     *
     * @param path
     * @return
     */
    public static MediaFileInfo getMediaFileInfo(String path, boolean isSupportAvFile) {
        String ext = getExtensionAsUpperCase(path);

        MediaFileInfo info = null;

        if (isAvFile(ext) && isSupportAvFile == true) {
            if (isAudioInFile(path)) {
                info = sExtensionToMediaFileTypeMap.get(ext + "_A");
            }
        } else if ("SCC".equals(ext)) {
            info = getSccFileInfo(path);
        }

        if (info == null) {
            info = sExtensionToMediaFileTypeMap.get(ext);
        }

        if (info == null) {
            info = sExtensionToMediaFileTypeMap.get("");
        }

        return info;
    }

    /**
     * <pre>
     * get MediaFileInfo by path and mimeType.
     * </pre>
     *
     * @param path
     * @return
     */
    public static MediaFileInfo getMediaFileInfo(String path, String mimeType) {
        String ext = getExtensionAsUpperCase(path);

        MediaFileInfo info = null;

        if (isAvFile(ext)) {
            if (isAudioMimeType(mimeType)) {
                info = sExtensionToMediaFileTypeMap.get(ext + "_A");
            }
        } else if ("SCC".equals(ext)) {
            info = getSccFileInfo(path);
        }

        if (info == null) {
            info = sExtensionToMediaFileTypeMap.get(ext);
        }

        if (info != null && info.fileType == FileType.DCF) {
            Integer fileType = sMimeTypeToFileTypeMap.get(mimeType);
            if (fileType != null) {
                info = sFileTypeToMediaFileTypeMap.get(fileType);
            }
        }

        if (info == null) {
            info = sExtensionToMediaFileTypeMap.get("");
        }

        return info;
    }

    /**
     * <pre>
     * get MediaFileInfo include DRM contents
     * </pre>
     *
     * @param path
     * @param context
     * @return
     */
    public static MediaFileInfo getMediaFileInfo(String path, Context context) {

        MediaFileInfo info = getMediaFileInfo(path, true);

        if ((info != null) && FileType.isDrmFileType(info.fileType)) {
            if (DrmUtils.isDRMFile(context, path)) {
                String mimeType = DrmUtils.getRealMimeTypeOfDRM(context, path);

                Integer type = sMimeTypeToFileTypeMap.get(mimeType);

                if (type == null) {
                    if (mimeType == null) {
                        Log.e(TAG, "getMediaFileInfoFromOpenFile - no file type for " + mimeType);
                        return null;
                    }
                } else {
                    info = sFileTypeToMediaFileTypeMap.get(type);
                }
            }
        }

        return info;
    }

    /**
     * <pre>
     * get MediaFileInfo of "SCC" file
     * It could be StoryAlbum or Scrap file
     * </pre>
     *
     * @param path
     * @return
     */
    private static MediaFileInfo getSccFileInfo(String path) {
        MediaFileInfo info = null;

        if (SccFileUtil.isSCCFile(path)) {
            String mimeType = SccFileUtil.getMimetypeFromSCCFile(path);

            if (mimeType != null) {
                if (mimeType.equals(MimeType.STORY_ALBUM)) {
                    info = sFileTypeToMediaFileTypeMap.get(FileType.SCC);
                } else if (mimeType.equals(MimeType.SCRAP_BOOK)) {
                    info = sFileTypeToMediaFileTypeMap.get(FileType.SCC_SCRAP);
                }
            }

        }

        return info;
    }

    private static boolean isAvFile(String ext) {
        boolean bRet = false;

        if (ext != null) {
            if ("MP4".equals(ext) || "3GP".equals(ext) || "3G2".equals(ext) || "ASF".equals(ext) || "3GPP".equals(ext)) {
                bRet = true;
            }
        }

        return bRet;
    }

    /**
     * <pre>
     * get MimeType of Media File(ex MP4, 3GP, ASF)
     * from MetaData
     * </pre>
     *
     * @param path
     * @return
     */
    private static String getMimeFromContent(String path) {
        String ret = null;
        File checkFile = SemFwWrapper.file(path);

        if (path != null && checkFile.exists() && checkFile.length() > 0) {
            MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
            try {
                mediaMetadataRetriever.setDataSource(path);
                ret = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
            } catch (Exception e) {
                Log.e(TAG, "Exception path - " + Log.getEncodedMsg(path));
                Log.e(TAG, "Exception:" + e.toString());
            } finally {
                mediaMetadataRetriever.release();
            }
        }
        return ret;
    }

    /**
     * <pre>
     * check mimetyps is start with "audio/"
     * </pre>
     *
     * @param mimeType
     * @return
     */
    private static boolean isAudioMimeType(String mimeType) {
        boolean bRet = false;

        if (mimeType != null) {
            bRet = mimeType.startsWith("audio/");
        }

        return bRet;
    }

    private static boolean isAudioInFile(String path) {
        String mimeType = getMimeFromContent(path);
        return isAudioMimeType(mimeType);
    }

    public static String getExtensionAsUpperCase(String path) {
        String ext = FileRecord.getExt(path);
        return ext.toUpperCase(Locale.ENGLISH);
    }

    public static String[] getDocumentExtensions() {
        int extensionSize = sDocumentExtensions.size();
        return sDocumentExtensions.toArray(new String[extensionSize]);
    }

    public static String getExtensionByMimeType(String mimeType) {
        String ret = null;

        Integer fileType = sMimeTypeToFileTypeMap.get(mimeType);
        if (fileType != null) {
            MediaFileInfo info = sFileTypeToMediaFileTypeMap.get(fileType);
            if (info != null) {
                ret = info.extension;
            }
        }
        return ret;
    }
}
